Skip to content

feat: async debounce - #150

Merged
frodi-karlsson merged 4 commits into
mainfrom
async-debounce
Jul 15, 2026
Merged

feat: async debounce#150
frodi-karlsson merged 4 commits into
mainfrom
async-debounce

Conversation

@frodi-karlsson

@frodi-karlsson frodi-karlsson commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

I recommend we have async versions of these to match asyncMemo

I ran into this with runInBackground as follows, in a way that broke at runtime:

class SomeService {
  @_Throttle(1000)
  async flush(): Promise<void> {
    // ...
  }
}

  // elsewhere:
 runInBackground(this.someService.flush())
  
  // runInBackground immediately does undefined.catch(...)

There's still a small quirk with this implementation:

const save = _asyncThrottle(..., 1000, { trailing: false })

const a = save('a')
const b = save('b') // Promise<undefined>

which is fine for the function variant, but for decorated variants it means

@_AsyncThrottle(1000, { trailing: false })
async someMethod(): Promise<number> { ... }

actually can return Promise<number | undefined> which is still better than returning a sync undefined

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds async-safe debounce/throttle utilities and method decorators so async functions always return Promises (aligning semantics with async memoization), plus a small refactor to centralize the MethodDecorator type.

Changes:

  • Add _asyncDebounce / _asyncThrottle to provide Promise-based coalescing semantics for async functions.
  • Add @_AsyncDebounce / @_AsyncThrottle decorators for async methods and extend test coverage for the new APIs.
  • Move the generic MethodDecorator<T> helper type to decorator.util.ts and update imports accordingly.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
packages/js-lib/src/decorators/memo.util.ts Removes the MethodDecorator type from memo utilities (relocated).
packages/js-lib/src/decorators/memo.decorator.ts Switches MethodDecorator import to decorator.util.ts.
packages/js-lib/src/decorators/decorator.util.ts Adds the shared generic MethodDecorator<T> type definition.
packages/js-lib/src/decorators/debounce.ts Refactors debounce timing state and introduces _asyncDebounce / _asyncThrottle.
packages/js-lib/src/decorators/debounce.test.ts Adds tests for _asyncDebounce / _asyncThrottle.
packages/js-lib/src/decorators/debounce.decorator.ts Adds @_AsyncDebounce / @_AsyncThrottle decorators for async methods.
packages/js-lib/src/decorators/debounce.decorator.test.ts Adds tests for the new async decorators.
packages/js-lib/src/decorators/asyncMemo.decorator.ts Switches MethodDecorator import to decorator.util.ts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/js-lib/src/decorators/debounce.ts
Comment thread packages/js-lib/src/decorators/debounce.ts Outdated
Comment thread packages/js-lib/src/decorators/debounce.decorator.ts Outdated
Comment thread packages/js-lib/src/decorators/decorator.util.ts Outdated
Comment thread packages/js-lib/src/decorators/debounce.test.ts
Comment thread packages/js-lib/src/decorators/debounce.decorator.test.ts

This comment was marked as resolved.

@frodi-karlsson
frodi-karlsson marked this pull request as ready for review July 15, 2026 14:55

@kirillgroshkov kirillgroshkov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I trust you that it works.

@frodi-karlsson
frodi-karlsson merged commit 5ef6799 into main Jul 15, 2026
3 checks passed
@frodi-karlsson
frodi-karlsson deleted the async-debounce branch July 15, 2026 15:14
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in @naturalcycles/js-lib-v15.84.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants